package com.rule.rest;

import com.google.common.base.Enums;
import com.rule.rule.engine.RuleEngine;
import com.rule.knowledge.base.models.Rule;
import com.rule.knowledge.base.KnowledgeBaseService;
import com.rule.rules.impl.insurance.ruleengine.InsuranceInferenceEngine;
import com.rule.rules.impl.insurance.ruleengine.InsuranceDetails;
import com.rule.rules.impl.insurance.ruleengine.PolicyHolderDetails;
import com.rule.rules.impl.loan.ruleengine.LoanDetails;
import com.rule.rules.impl.loan.ruleengine.LoanInferenceEngine;
import com.rule.rules.impl.loan.ruleengine.UserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 规则引擎 REST API
 */
@Slf4j
@RestController
public class RuleEngineRestController {
    @Autowired
    private KnowledgeBaseService knowledgeBaseService;
    @Autowired
    private RuleEngine ruleEngine;
    @Autowired
    private LoanInferenceEngine loanInferenceEngine;
    @Autowired
    private InsuranceInferenceEngine insuranceInferenceEngine;

    //按规则名称空间获取指定的规则
    @RequestMapping(value = "/get-all-rules/{ruleNamespace}",method = RequestMethod.GET)
    public ResponseEntity<?> getRulesByNamespace(@PathVariable("ruleNamespace") String ruleNamespace) {
        RuleNamespace namespace = Enums.getIfPresent(RuleNamespace.class, ruleNamespace.toUpperCase()).or(RuleNamespace.DEFAULT);
        List<Rule> allRules = knowledgeBaseService.getAllRuleByNamespace(namespace.toString());
        return ResponseEntity.ok(allRules);
    }

     //获取所有规则
    @RequestMapping(value = "get-all-rules",method = RequestMethod.GET)
    public ResponseEntity<?> getAllRules() {
        List<Rule> allRules = knowledgeBaseService.getAllRules();
        return ResponseEntity.ok(allRules);
    }

    /**
     * 贷款
     *
     * @param
     * @return
     */
    @PostMapping(value = "/loan")
    public ResponseEntity<?> postUserLoanDetails(@RequestBody UserDetails userDetails) {
        LoanDetails result = (LoanDetails) ruleEngine.run(loanInferenceEngine, userDetails);
        return ResponseEntity.ok(result);
    }
    /**
     * 保险
     * @param
     * @return 
     */
    @PostMapping(value = "/insurance")
    public ResponseEntity<?> postCarLoanDetails(@RequestBody PolicyHolderDetails policyHolderDetails) {
        InsuranceDetails result = (InsuranceDetails) ruleEngine.run(insuranceInferenceEngine, policyHolderDetails);
        return ResponseEntity.ok(result);
    }
}
